import asyncio
import aiomysql
import com.gcteamo.coj.config as config

class Database:
    def __init__(self):
        self.conn_pool=None
    async def Init(self):
        try:
            self.conn_pool = await aiomysql.create_pool(
                host='gcteamo.com',
                port=3306,
                user='my',
                password='123456',
                db='my'
            )
            config.logger.info("Init the database successfully")
        except Exception as e:
            config.logger.critical(e)
    async def execute(self,conn,sql):
        async with conn.cursor(aiomysql.DictCursor) as cur:
            await cur.execute(sql)
            return cur
    async def GetUid(self,username):
        conn = await self.conn_pool.acquire()
        cur = await self.execute(conn,"SELECT `id`,`name` FROM `user` WHERE `name` = '{a}';".format(a=username))
        if cur.rowcount == 0:
            return -1
        r = await cur.fetchone()
        return r['id']
    async def GetUserPwd(self,uid):
        conn = await self.conn_pool.acquire()
        cur = await self.execute(conn, "SELECT `id`,`password` FROM `user` WHERE `id` = '{a}';".format(a=uid))
        r = await cur.fetchone()
        return r['password']

if __name__ == '__main__':
    pass